package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.sys.entity.NotifyLog1;
import com.dataseek.iot.sys.entity.NotifyLogAlarmTypeNum;
import com.dataseek.iot.sys.mapper.NotifyLogMapper1;
import com.dataseek.iot.sys.service.NotifyLogService;
import com.dataseek.iot.sys.vo.NotifyLogVo1;
import com.dataseek.iot.toolkit.auth.entity.SysUser;
import com.dataseek.iot.toolkit.auth.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service
@Slf4j
public class NotifyLogServiceImpl extends ServiceImpl<NotifyLogMapper1, NotifyLog1> implements NotifyLogService {


    @Autowired
    private NotifyLogMapper1 notifyLogeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public NotifyLogVo1 queryAction(String userId, String status, Integer num, Integer size) {

        NotifyLogVo1 logVo = new NotifyLogVo1();
        QueryWrapper<NotifyLog1> qw = new QueryWrapper<>();
        if (userId != null && !userId.isEmpty()) {
            qw.eq("user_id", userId);
        }

        if (status != null && !status.isEmpty()) {
            qw.eq("status", status);
        }

        qw.orderBy(true, false, "create_time");
        IPage<NotifyLog1> page = new Page<>(num, size);
        List<NotifyLog1> mapList = notifyLogeMapper.selectPage(page, qw).getRecords();
        logVo.setPage(num);
        logVo.setSize(size);
        logVo.setTotal(page.getTotal());

        //获取userName
        QueryWrapper<SysUser> qwUser = new QueryWrapper<>();
        for (NotifyLog1 tmplog : mapList) {
            qwUser.eq("user_id", Integer.parseInt(tmplog.getUserId()));
            SysUser tmpUser = sysUserMapper.selectOne(qwUser);
            if (tmpUser != null) {
                tmplog.setUserName(tmpUser.getUserName());
            }
        }

        logVo.setList(mapList);
        return logVo;
    }

    @Override
    public void insertAction(NotifyLog1 notifyLog1) {
        this.saveOrUpdate(notifyLog1);
    }

    @Override
    public Integer updateAction(NotifyLog1 notifyLog1) {
        notifyLog1.setUpdateBy("master");
        int resultCode = notifyLogeMapper.updateById(notifyLog1);
        return resultCode;
    }

    @Override
    public List<NotifyLogAlarmTypeNum> queryTypeNum() {
        List<NotifyLogAlarmTypeNum> reList = new ArrayList<>();
        QueryWrapper<NotifyLog1> qw = new QueryWrapper<>();
        qw.ge("create_time", this.getFirstDayOfMonth());
        List<NotifyLog1> queList = this.list(qw);
        if (!CollectionUtils.isEmpty(queList)) {
            Map<String, List<NotifyLog1>> queMap = queList.stream().collect(Collectors.groupingBy(NotifyLog1::getAlarmType));
            for (Map.Entry<String, List<NotifyLog1>> en : queMap.entrySet()) {
                NotifyLogAlarmTypeNum typeDto = new NotifyLogAlarmTypeNum();
                typeDto.setTypeName(this.transType(en.getKey()));
                typeDto.setCnt(en.getValue().size());
                reList.add(typeDto);
            }
        }
        return reList;
    }

    private String transType(String type) {
        String typeName;
        switch (type) {
            case "A":
                typeName = "上限报警";
                break;
            case "B":
                typeName = "上上限报警";
                break;
            case "C":
                typeName = "下限报警";
                break;
            case "D":
                typeName = "下下限报警";
                break;
            default:
                typeName = "其他";
        }
        return typeName;
    }


    //获取每月第一天
    private Date getFirstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return  calendar.getTime();
    }

}
